//
//  WBStatusPictureView.swift
//  SwiftWeibo
//
//  Created by Lausen on 2017/4/26.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {

    @IBOutlet weak var pictureViewBottomCons: NSLayoutConstraint!

    var viewModel: WBStatusViewModel?{
        
        didSet{
            
            caculPictureViewSize()
        }
    }
    
    private func caculPictureViewSize() {
        
        ///如果是单张图,设置图片视图的大小
        if viewModel?.picURLS?.count == 1 {
            
            let iv = subviews[0]
            
            let size = viewModel?.statusPictureSize ?? CGSize()
            
            
            iv.frame = CGRect(x: 0,
                              y: WBStatusPictureViewOutterMargin,
                              width: size.width,
                              height: size.height - 12)
            
        }else{ //如果是多张图,一定要回复一下,第一张图的宽高
            
            let iv = subviews[0]
            
            iv.frame = CGRect(x: 0,
                              y: WBStatusPictureViewOutterMargin,
                              width: itemWidth,
                              height: itemWidth)
        }
        
        pictureViewBottomCons.constant = viewModel?.statusPictureSize.height ?? 0
    }
    
    var urls: [WBStatusPicture]?{
        
        didSet{
            
            for v in subviews{
                
                v.isHidden = true
            }
            
            var index = 0
            
            for statusPicture in urls ?? []{
                
                //1:根据下标获取图片视图
                let imageView = subviews[index] as! UIImageView
                imageView.contentMode = .scaleAspectFill
                imageView.clipsToBounds = true
                
                if index == 1 && urls?.count == 4 {
                    
                    index += 1
                }
                
                //2:给图片视图赋值
                imageView.lau_image(urlString: statusPicture.thumbnail_pic, placeholderImage: nil)
                
                //3:显示图片视图
                imageView.isHidden = false
                
                index += 1
            }
        }
    }
    
    
    override func awakeFromNib() {
        
        setupUI()
    }
}

// MARK: - 设置UI
extension WBStatusPictureView{

    fileprivate func setupUI(){
        
        /// 设置背景颜色和父视图的背景颜色一致
        backgroundColor = superview?.backgroundColor
        
        clipsToBounds = true
        
        let rect = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: itemWidth, height: itemWidth)

        for i in 0..<9 {
            
            /// 行
            let row = CGFloat(i / 3)
            /// 列
            let col = CGFloat(i % 3)
            
            let iv = UIImageView()
            
            iv.frame = rect.offsetBy(dx: col * (WBStatusPictureViewInnerMargin + itemWidth), dy: row * (itemWidth + WBStatusPictureViewInnerMargin))
            
            addSubview(iv)
        }
    }
}
